package com.tzbank.wisdom.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.dao.BusinessMapper;
import com.tzbank.wisdom.entity.AccessBankInfo;
import com.tzbank.wisdom.entity.BankNetwork;
import com.tzbank.wisdom.service.TransactionService;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service(value = "transactionService")
public class TransactionServiceImpl implements TransactionService {

	@Autowired
    RestTemplate restTemplate;
	
	@Autowired
    private BusinessMapper businessMapper;
	

	
	/**
	 * 根据银行号判断银行是否是超级网银
	 * @param body
	 * @return
	 */
	public Map<String,Object> superOnlineBank(Map<String,String> body){
		Map<String,Object> resultMap = new HashMap<String,Object>();		//返回数据集合
		String bankNo = body.get("bankNo");			//银行号
		String bankName = body.get("bankName");		//银行名称
		//参数验证
		if(StringUtils.isBlank(bankNo)) {
			return ResultUtil.resultMap(CodeUtil.CODE_10000.getName(), CodeUtil.CODE_10000.getCode(), null);
		}
		List<AccessBankInfo> list = businessMapper.getAccessBankInfo(bankNo);		//查询超级网银
		resultMap.put("flag", false);		//不是超级银行标记
		if(list != null && list.size() > 0) {
			String[] bankNos = {"313","314","320","323","402"}; 		//农商行村镇银行类多个的
			boolean manyFlag = false;			//多银行标记
			for(String no : bankNos) {
				//判断银行号是否满足上面的集合
				if(bankNo.equals(no)) {
					manyFlag = true;		
					break;		//跳出for循环
				}
			}
			if(manyFlag) {
				if(StringUtils.isNotBlank(bankName) ) {
					String bankNameStr = bankName.substring(0, bankName.indexOf("银行"))	;		//银行名称截断
					for(AccessBankInfo abi : list) {
						//判断网银结算账号是否和银行名称有关系
						if(abi.getBankName().contains(bankNameStr)) {
							resultMap.put("superBankId", abi.getBankId());		//超级银行号
							resultMap.put("bankName", abi.getBankName());
							resultMap.put("flag", true);		//超级银行标记
							break;		//跳出for循环
						}
					}
				}
			}else {
				AccessBankInfo accessBankInfo= list.get(0);		//获取第一个
				resultMap.put("superBankId", accessBankInfo.getBankId());		//超级银行号
				resultMap.put("bankName", accessBankInfo.getBankName());
				resultMap.put("flag", true);		//超级银行标记
			}
		}
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), resultMap);
	}
	
	/**
     * 根据银行号、城市编号查询银行网点
     * @param bodyMap
     * @return
     */
	public Map<String,Object> getBankNetworkByBankId(Map<String, String> bodyMap){
		String bankNetworkNo = bodyMap.get("bankNetworkNo");			//银行网点号
		String bankNo = bodyMap.get("bankNo");			//银行号
		String cityCode = bodyMap.get("cityCode");			//城市编号
		Map<String,Object> resultMap = new HashMap<String,Object>();		//返回集合
		if(StringUtils.isNotBlank(bankNetworkNo)) {
			BankNetwork bn = businessMapper.getBankNetworkByBankId(bankNetworkNo);		//查询单网点
			resultMap.put("network", bn);
			return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), resultMap);
		}
		
		if(StringUtils.isBlank(bankNo) && StringUtils.isBlank(cityCode)) {
			//参数判断
			return ResultUtil.resultMap(CodeUtil.CODE_10000.getName(), CodeUtil.CODE_10000.getCode(), null);
		}
		List<BankNetwork> list = businessMapper.getListBankNetwork(bankNo, cityCode);		//查询网点
		resultMap.put("list", list);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), resultMap);
    }
	
	/**
	 * 转账手续费查询
	 * @param body
	 * @return
	 */
	public Map<String,Object> queryTranFee(Map<String,String> body){
		String clientSeqNo = body.get("clientSeqNo");		//客户号
		
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				   TransCodeEnum.DIFFERENTBANK_TRAN_FREE.getServiceCode(),
				   TransCodeEnum.DIFFERENTBANK_TRAN_FREE.getServiceSence(), 
				   "", SysConstans.ABP_CODE, "","301010");			//targetSysId  核心是301010
		
		
		//-------------公共报文体------------
		Map<String, Object> inputMap = RequestMap.getInputUserInfo(clientSeqNo);
		inputMap.put("sysHead", reqSysHead);
		
		inputMap.put("payRule", "0");				//支付规则
		inputMap.put("ccy", "RMB");						
		inputMap.put("openBranch", "0110");			//开户机构
		inputMap.put("payeeBankId", body.get("payeeBankId"));		//收款行行号
		inputMap.put("feeType", "F100");			//费用类型
		inputMap.put("priority", "norm");			//优先级
		inputMap.put("amt", body.get("amt"));		//手续费金额
				
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);		//接口调用
		//判断是否调用成功
		if(CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			Map<String,Object> dataMap = (Map<String,Object>) resultMap.get("data");
			JSONObject head = JSONObject.parseObject(JSONObject.toJSONString(dataMap.get("head")));		//获取head的对象
			String targetSysId = head.getString("targetSysId");		//目标系统号
			JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(dataMap));		//获取body的对象
			String ptChannelType = data.getString("ptChannelType");		//支付通道标识
			String commission = data.getString("commission");			//手续费金额
			String paidCommission = data.getString("paidCommission");		//实收手续费金额
			
			//封装的返回数据对象
			Map<String,String> realResultData = new HashMap<String,String>();
			realResultData.put("targetSysId", targetSysId);			
			realResultData.put("ptChannelType", ptChannelType);
			realResultData.put("commission", paidCommission);			//手续费金额  做入参参数转换
			realResultData.put("origCommission", commission);	//手续费金额  做入参参数转换
			
			resultMap.put("data", realResultData);		//重新赋值
		}
		return resultMap;
	}
	

	/**
	 * 转账
	 * @param body
	 * @return
	 */
	public Map<String,Object> transfer(Map<String,String> paramsMap){
		String clientSeqNo = paramsMap.get("clientSeqNo");		//客户号
		String tranType = paramsMap.get("tranType");		//交易类型
		Map<String, Object> inputMap = RequestMap.getInputUserInfo(clientSeqNo);
		Map<String, Object> reqSysHead = null;
		//固定部分
		inputMap.put("codeId", "CNY");		//代码编号 CNY       **********
		inputMap.put("amt",paramsMap.get("amt"));		//交易金额(金额)       **********
		inputMap.put("isAddPayee",paramsMap.get("isAddPayee"));		//是否添加常用收款人       **********
		//根据转账类型判断调用的接口 1、行内转账 2、跨行快速转账 3、跨行大额转账
		if("01".equals(tranType)) {
			//--------------------------行内转账----------------------------
			reqSysHead =RequestMap.getReqSysHead(
					   TransCodeEnum.SAMEBANK_TRANSFER.getServiceCode(),
					   TransCodeEnum.SAMEBANK_TRANSFER.getServiceSence(), 
					   "", 
					   SysConstans.ABP_CODE, 
					   "");
			//-------------业务报文体------------
			inputMap.put("crCardNo",paramsMap.get("crCardNo"));			//贷方账卡号  转出账号       **********
			inputMap.put("crAcctName",paramsMap.get("crAcctName"));		//贷方账户名称       **********
			inputMap.put("crAcctType","PSAV");		//贷方账户类型       **********
			inputMap.put("drCardNo",paramsMap.get("drCardNo"));			//借方账卡号  转入账号       **********
			inputMap.put("drAcctName",paramsMap.get("drAcctName"));		//借方账户名称       **********
			inputMap.put("payPurpuse",paramsMap.get("payPurpuse"));		//付款用途       **********
			inputMap.put("tranPassword",paramsMap.get("tranPassword"));	//交易密码       **********
			inputMap.put("ccTtFlag","C");		//同行 钞汇标志 C       **********
			inputMap.put("enrtyType",paramsMap.get("enrtyType"));		//到账方式       **********
			
			
			/**
		  		"tranType":"01",
  				"crCardNo":"6224271190391122-101",
  				"crAcctName":"缘尽",
  				"drCardNo":"6224271190391106",
  				"drAcctName":"开户测试弄",
  				"payPurpuse":"01",
  				"tranPassword":"MDAwMDAxNDABAgAAAWgAAACkAAAN+Pc9n9jyFK4ECj/gRPiBZ1hc4CymOO0iNGqiJG3c7Ei7oOzRXl9OjIj6M9WzIBcrpQV8I4EiUFLAKfeR65X3mveJB8gOTx4OdplS4U3kJvsHcC9YytHhsW2g5pqpTEnXc3XqA2k1W/Pe9ecYBZDmYj3omWlsEHWD7gyRz7MXfTAwMDAwMDIxjeqgSL3rHag5a5MLkZmf5yQ8UtHy",
  				"amt":"500",
  				"isAddPayee":true,
  				"enrtyType":"0",
			 */
			
		}else {
			if("02".equals(tranType)) {
				//跨行大额转账
				reqSysHead =RequestMap.getReqSysHead(
						   TransCodeEnum.DIFFERENTBANK_TRANSFER.getServiceCode(),
						   TransCodeEnum.DIFFERENTBANK_TRANSFER.getServiceSence(), 
						   "",SysConstans.ABP_CODE,"");
				inputMap.put("bankOutTranFlag","INTERCITY");							//跨行大额标记
				inputMap.put("origCommission",paramsMap.get("origCommission"));			//原手续费金额	==手续费返回的COMMISSION     **********
				inputMap.put("ptChannelType",paramsMap.get("ptChannelType"));			//支付通道标识		      **********
				inputMap.put("targetSysId",paramsMap.get("targetSysId"));				//目标系统ID	      **********
			}
			if("03".equals(tranType)) {
				//跨行快速转账
				reqSysHead =RequestMap.getReqSysHead(
						   TransCodeEnum.DIFFERENTBANK_FAST_TRANSFER.getServiceCode(),
						   TransCodeEnum.DIFFERENTBANK_FAST_TRANSFER.getServiceSence(), 
						   "",SysConstans.ABP_CODE,"");
				inputMap.put("bankOutTranFlag","SOBS");								//跨行快速标记
				
				
			}
			//-------------业务报文体------------
			inputMap.put("tranOutAcctNo",paramsMap.get("crCardNo"));				//转出账号
			inputMap.put("tranOutAcctName",paramsMap.get("crAcctName"));			//转出账户名称
			inputMap.put("payerAcctType","PSAV");									//付款人账户类型  
			inputMap.put("tranInAcctNo",paramsMap.get("drCardNo"));					//转入账号
			inputMap.put("appoInAcctName",paramsMap.get("drAcctName"));				//转入账户户名
			inputMap.put("password",paramsMap.get("tranPassword"));					//密码
			inputMap.put("payeeBankId",paramsMap.get("payeeBankId") + "");			//收款行行名      大额为银行id/快速为超级网银号      **********
			inputMap.put("subBranchId",paramsMap.get("subBranchId") + "");			//收款行网点      **********
			inputMap.put("caTtFlag","C");			//跨行 钞汇标志 C      **********
			inputMap.put("payPurpuse","往来款");		//转账用途		      **********
			inputMap.put("commission",paramsMap.get("commission"));				//手续费金额  ==手续费返回的PAID_COMMISSION		   **********
		}
		
		//-------------公共报文体------------
		inputMap.put("sysHead", reqSysHead);
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequestNoHead(inputMap);		//接口调用
		return resultMap;
	}
	
	public static void main(String[] args) {
		String aa = "台州银行";
		System.out.println(aa.substring(0, aa.indexOf("银行") ) );	
				
	}
}
